package com.xiangban.yanglaodajie_platform.service;

import com.xiangban.yanglaodajie_platform.dao.UserDeviceMapper;
import com.xiangban.yanglaodajie_platform.entity.UserDevice;
import com.xiangban.yanglaodajie_platform.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserDeviceService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private UserDeviceMapper userDeviceMapper;

    public void insertUserDevice(UserDevice userDevice) {
        userDevice.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        List<UserDevice> userDeviceByUserIds = userDeviceMapper.getUserDeviceByUserId(userDevice);
        if (userDeviceByUserIds.size() == 0) {
            userDeviceMapper.insertUserDevice(userDevice);
        } else {
            if (userDeviceByUserIds.size() > 1) {
                userDeviceMapper.delUserDeviceByUserId(userDevice);
            }
            UserDevice userDeviceByUserId = userDeviceByUserIds.get(0);
            if (!userDeviceByUserId.getUserId().equals(userDevice.getUserId()) || !userDeviceByUserId.getClientid().equals(userDevice.getClientid())) {
                userDeviceMapper.updateUserDeviceByUserId(userDevice);

            }


        }
    }
}
